package com.acegear.horizon.controllers;

import com.acegear.horizon.controllers.vo.ResultVO;
import com.acegear.horizon.domain.models.Daily;
import com.acegear.horizon.domain.repository.DailyRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

/**
 * Created by wangsike on 2016/12/22.
 */
@RestController
@RequestMapping("/daily")
@Transactional
public class DailyController {

    @Autowired
    private DailyRepository dailyRepository;

    @RequestMapping(value = "/today", method = RequestMethod.GET)
    public ResultVO todayDaily(@RequestHeader(value = "X-Consumer-Username", required = false, defaultValue = "0") Long viewerId) {
        return dailyRepository
                .current().map(daily -> new ResultVO<>(daily.getDailyDetailVO(viewerId)))
                .orElse(ResultVO.EMPTY);
    }

    @RequestMapping(value = "/next", method = RequestMethod.GET)
    public ResultVO nextDay(Long dailyId,
                            @RequestHeader(value = "X-Consumer-Username", required = false, defaultValue = "0") Long viewerId) {
        return dailyRepository
                .next(dailyId).map(daily -> new ResultVO<>(daily.getDailyDetailVO(viewerId)))
                .orElse(ResultVO.EMPTY);
    }

    @RequestMapping(value = "/previous", method = RequestMethod.GET)
    public ResultVO previousDay(Long dailyId,
                                @RequestHeader(value = "X-Consumer-Username", required = false, defaultValue = "0") Long viewerId) {
        return dailyRepository
                .previous(dailyId).map(daily -> new ResultVO<>(daily.getDailyDetailVO(viewerId)))
                .orElse(ResultVO.EMPTY);
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultVO dailyList(Integer page, Integer count) {
        Pageable pageable = new PageRequest(page, count, Sort.Direction.DESC, "dailyId");
        return new ResultVO<>(dailyRepository
                .find(pageable)
                .map(Daily::getDailyVO)
                .collect(Collectors.toList()));
    }

    @RequestMapping(value = "/{dailyId}", method = RequestMethod.GET)
    public ResultVO dailyById(@PathVariable Long dailyId,
                              @RequestHeader(value = "X-Consumer-Username", required = false, defaultValue = "0") Long viewerId) {
        return dailyRepository
                .findByDailyId(dailyId)
                .map(daily -> new ResultVO<>(daily.getDailyDetailVO(viewerId)))
                .orElse(ResultVO.DAILY_NOT_FOUND);
    }
}